/* JavaScript Document
 *
 * Developer: 		Rynelle Coronacion
 * Freelance: 		WebsiGn Studio
 * Contact Number: 	+639165290788 / +639198003708
 * Email ADdress: 	rynellecoronacion@gmail.com / rynellecoronacion@yahoo.com
 * Facebook:		www.facebook.com/rynelle.coronacion
 *
 */

		function showMouseSettings() {
			$('#mouseover_settings').show();
		}

		function hideMouseSettings() {
			$('#mouseover_settings').fadeOut();
		}
		
		function popupRequest(action, request_page, sub_title, iframe_height, iframe_width, str_id) {
			
			if ( action == 'show' ) {
				
				if (str_id != '')
					var request_page_id = request_page+'&id='+str_id;
				else
					var request_page_id = request_page;
					
				var printPreviewButton = '<div class="rp-body-button">'+
											'<table width="0" border="0" cellspacing="0" cellpadding="0">'+
												'<tr>'+
													'<td width="85" valign="middle"><label id="print" onclick="printIframe(send_request);" class="cursor-pointer"><img src="./application/images/icon/print.png" border="0"/> PRINT</label></td>'+
													'<td width="12" class="td-border-left-right"></td>'+
													/*'<td width="165" valign="middle">{exportButton}</td>'+*/
												'</tr>'+
											'</table>'+
										  '</div>';
										  
				switch(request_page) {
					case 'PrintStudentSearch':
					case 'PrintStudentGradesSearch':
					case 'EvaluationChecklist':
					case 'SchedulePerSection': 		
					case 'SubjectPrintPreview': 		previewButton = printPreviewButton; overflow = 'overflow:hidden;'; scrolling = ' scrolling="no"'; break;
					case 'ProfessorAssignSubject': 		previewButton = ''; 				overflow = ''; scrolling = ' scrolling="auto"'; break;
					case 'StudentCurrentSubjectTaken': 	previewButton = printPreviewButton; overflow = ''; scrolling = ' scrolling="auto"'; break;
					
					default: 							previewButton = ''; 				overflow = 'overflow:hidden;'; scrolling = ' scrolling="no"'; break;
					
				}
				
				var htmlPopup = '<div class="popup-main-blocker" onclick="popupRequest(\'hide\');"></div>'+
								'<div class="popup-main">'+
									'<div class="popup-main-close"><img src="./application/images/close_pop_up.png" border="0" onclick="popupRequest(\'hide\');" class="cursor-pointer"/></div>'+
									'<div class="popup-main-content">'+
										'<div class="popup-content">'+
											'<div class="popup-content-header"><span id="popup_title">'+ sub_title +'</span></div>'+
											previewButton +
											'<div class="popup-content-iframe" id="popup_content_iframe">'+
												'<iframe name="send_request" id="send_request" ' + scrolling + ' class="iframe-view" src="frame.php?page='+request_page_id+'" style="height:' + iframe_height + 'px; width:' + iframe_width + 'px; ' + overflow + '"></iframe>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>';
								
				$('#popup_request').show().html(htmlPopup);
				$('.popup-main').each(function(){
					$(this).css('left',($(window).width()-$(this).outerWidth())/ 2 + 'px');
					$(this).css('top',($(window).height()-$(this).outerHeight())/ 2 + 'px');
				});
			}
			else {
				$('#popup_request').hide();
				$('#mouseover_settings').hide();
			}
		}
		
			function printIframe(iframePrintPreview)  {
				iframePrintPreview.focus();
				iframePrintPreview.print();
			} 
			
		function numberDate(e, decimal) {
			
			var key;
			var keychar;
			
			if (window.event) {
			   key = window.event.keyCode;
			}
			else if (e) {
			   key = e.which;
			}
			else {
			   return true;
			}
			
			keychar = String.fromCharCode(key);
			
			//alert(key);
			
			if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) || (key==47) ) {
			   return true;
			}
			else if ((("0123456789.").indexOf(keychar) > -1)) {
			   return true;
			}
			else if (decimal && (keychar == ".")) { 
			  return true;
			}
			else
			   return false;
		}
		
		function is_int(value){ 
			if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
				return true;
			} else { 
				return false;
			} 
		}
		
		function refreshPost() {
			window.location.href = window.location.href;
		}
		
		function homePageLink() {
			window.location.href = './';
		}
		
		function linkNewPage(strPageURL) {
			window.location.href = '?page='+strPageURL;
		}
		
		function logout() {
			window.location.href = '?page=Logout';
		}
		
		function searchBox(strDivID) {
			
			var strSearchBox 		= $('#'+strDivID);
			
			if (strSearchBox.val() != 'Search here...') {
				strSearchBox.removeClass('login-color-aaa').addClass('login-color-000');
			}
			
			strSearchBox.focus(function () {
				if ( strSearchBox.val() == 'Search here...' ) {
					strSearchBox.val('').removeClass('login-color-aaa').addClass('login-color-000');
				}
				else if (strSearchBox.val() == '') {
					strSearchBox.removeClass('login-color-aaa').addClass('login-color-000');
				}
				else {
					strSearchBox.removeClass('login-color-aaa').addClass('login-color-000');
				}
			});
			strSearchBox.blur(function() {
				if ( strSearchBox.val() == '' ) {
					$(strSearchBox).val('Search here...').removeClass('login-color-000').addClass('login-color-aaa');
				}
			});
		}
		
		/* Change Image Mouse Over */
		function mouseImage(divID, srcImage) {
			$('#'+divID).attr('src', $('#'+srcImage).attr('src'));
		}
		
		
		/* DELETE SMALL POP UP */
		function popupDelete(strID, popupWidth) {
			$('#delete_button_'+strID).show().css('width', popupWidth);
		}
		
		function popupDeleteHide(strID) {
			$('#delete_button_'+strID).hide();
		}
		
		function deleteAccount(strPostID, strPOST) {
			$.post('request.php', {AJAXPost:strPOST, strID:strPostID}, function() {
				window.location.href = window.location.href;
			});
		}
		
		function setTimeSchedule(str_year) {
			var table = '<table width="0" border="0" cellspacing="0" cellpadding="0" class="schedule-table" id="schedule_table">'+
						 '<tr>'+
							'<td width="150" colspan="3" height="25" class="schedule-label schedule-label-right">&nbsp;</td>'+
							'<td width="100" class="schedule-label schedule-label-right">Monday</td>'+
							'<td width="100" class="schedule-label schedule-label-right">Tuesday</td>'+
							'<td width="100" class="schedule-label schedule-label-right">Wenesday</td>'+
							'<td width="100" class="schedule-label schedule-label-right">Thursday</td>'+
							'<td width="100" class="schedule-label schedule-label-right">Friday</td>'+
							'<td width="100" class="schedule-label">Saturday</td>'+
						 '</tr>';
			table += '</table>'+
					 '<div class="preview-schedule" style="margin-top:8px;"><div onclick="appendTableTimeSchedule(\'' + str_year + '\'); "><span id="save_schedule">INSERT TIME</span></div></div>'+
					 '<div style="margin-top:8px; margin-bottom:8px; background-color:#FCC; text-align:center; font-size:14px; border:1px solid #eee; padding:5px;">'+
						'<div>'+
							'<span id="save_schedule_loading_done" onclick="doneTableTimeSchedule(\'' + str_year + '\'); " class="cursor-pointer" style="color:#00F; font-weight:bold; font-size:12px; text-decoration:underline;">DONE</span>'+
						'</div>'+
						'<div id="preview_schedule_waiting" class="preview-schedule-waiting display-none" style="font-size:11px;">'+
							'<img src="./application/images/icon/loading_round.gif" border="0" /> Please wait. This will take few minutes...'+
						'</div>'+
					 '</div>';
					
			$('#set_time_schedule').html(table);
			$('.set-time-schedule').text('Temporary Time Schedule').attr('onclick', false);
						 
			 $.post('request.php', {AJAXPost:'getTempSchedule', year_id:str_year}, function(result) {
				 $.each(result, function(key, data) {
				  tr = '<tr id="sched_time_' + data['trigger_id'] + '">'+
								'<td width="65" height="75" class="schedule-label-time" align="center">' + data['sched_from'] + '</td>'+
								'<td width="20" class="schedule-label-time" align="center">-</td>'+
								'<td width="65" class="schedule-label-time schedule-label-right" align="center">' + data['sched_to'] + '&nbsp;&nbsp;<img style="cursor:pointer" onclick="deleteTempSchedule(\'' + data['trigger_id'] + '\')" title="Cancel" src="./application/images/icon/close.png" border="0"/></td>'+
								'<td width="100" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td width="100" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td width="100" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td width="100" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td width="100" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td width="100" class="schedule-label-time">&nbsp;</td>'+
							 '</tr>';
					$('#schedule_table').append(tr);
				 });
			 }, 'json');
						 
			
		}
		
		function changeTimeScheduleActive(str_year) {
			$('#schedule_change_time').html('<div id="time_schedule_waiting" style="text-decoration:none; font-size:12px; cursor:default;"><img src="./application/images/icon/loading_round.gif" border="0" /> Please wait...</div>');
			$.post('request.php', {AJAXPost:'ChangeTimeScheduleActive', year_id:str_year}, function() {
				window.location.href = window.location.href;
			});	
		}
		
		function doneTableTimeSchedule(str_year) {
			
			$('#preview_schedule_waiting').show();
			$('#save_schedule_loading_done').hide();
			$('#schedule_table').hide();
			$('.preview-schedule').hide();
			
			$.post('request.php', {AJAXPost:'saveTempScheduleToLive', year_id:str_year}, function() {
				window.location.href = window.location.href;
			});
		}
		
		function deleteTempSchedule(triggerID) {
			 $.post('request.php', {AJAXPost:'deleteTempSchedule', trigger_id:triggerID}, function() {
				 $('#sched_time_'+triggerID).hide();
				 $('.preview-schedule').show();
			 });
		}
		
		function selectTimeInterval() {
			
			var totalSec = 25200 + 900;
			var hours = parseInt( totalSec / 3600 ) % 24;
			var minutes = parseInt( totalSec / 60 ) % 60;
			
			var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes);
			
			alert(result);
			
		}

		function schedTimeResult(sched_to) {
			return sched_to;
		}
		
		function appendTableTimeSchedule(str_year) {

			$.post('request.php', {AJAXPost:'getTempLastSchedule', year_id:str_year}, function(res) {
				

				$('.preview-schedule').hide();
				table = '<tr class="table-schedule-tr-insert">'+
								'<td height="75" colspan="3" class="schedule-label-time schedule-label-right" align="center">';
								table += '<table width="0" border="0" cellspacing="0" cellpadding="0" align="center">'+
												 '<tr>';
										   table += '<td width="50" height="25">&nbsp;START</td>';
										   table += '<td width="10"> : </td>';
										   table += '<td width="80">';
										   			start_from 	= 21600;
													end_from   	= 79200;
													table += '<select id="sched_from" class="select-schedule-time" style="border:1px solid #CCC; padding:1px; font-size:12px; width:80px; border-radius:3px; margin-right:5px;">';
														while(start_from <= end_from) {
															var hours_from = parseInt( start_from / 3600 ) % 24;
															var minutes_from = parseInt( start_from / 60 ) % 60;
															
															if ( hours_from < 10 ) {
																display_hours_from = "0" + hours_from;
															}
															else {
																/*if ( hours_from > 12 ) {
																	dis_hours_from = parseInt(hours_from - 12);
																	if ( dis_hours_from < 10 ) {
																		display_hours_from = "0" + dis_hours_from;
																	}
																	else {
																		display_hours_from = dis_hours_from;
																	}
																}
																else {*/
																	display_hours_from = hours_from;
																/*}*/
															}
															
															display_minutes_from = (minutes_from < 10 ? "0" + minutes_from : minutes_from);
															table += '<option value="' + display_hours_from + ":" + display_minutes_from + '?'+ start_from +'"><span id="interval_from"></span>' + display_hours_from + ":" + display_minutes_from + ( hours_from >= 12 ? ' pm' : ' am') + '</option>';
															start_from += 900;
														}
													table += '</select>';
										   table += '</td>';
									    table += '</tr>';
									    		 '<tr>';
										   table += '<td width="50" height="25">&nbsp;END</td>';
										   table += '<td width="10"> : </td>';
										   table += '<td width="85">';
										  		start_to 	= 25200;
												end_to   	= 79200;
												table += '<select id="sched_to" class="select-schedule-time" style="border:1px solid #CCC; padding:1px; font-size:12px; width:80px; border-radius:3px; margin-right:5px;">';
													while(start_to <= end_to) {
														var hours_to = parseInt( start_to / 3600 ) % 24;
														var minutes_to = parseInt( start_to / 60 ) % 60;
														
														if ( hours_to < 10 ) {
															display_hours_to = "0" + hours_to;
														}
														else {
															/*if ( hours_to > 12 ) {
																dis_hours_to = parseInt(hours_to - 12);
																if ( dis_hours_to < 10 ) {
																	display_hours_to = "0" + dis_hours_to;
																}
																else {
																	display_hours_to = dis_hours_to;
																}
															}
															else {*/
																display_hours_to = hours_to;
															/*}*/
														}
														 
														 display_minutes_to = (minutes_to < 10 ? "0" + minutes_to : minutes_to);
														table += '<option value="' + display_hours_to + ":" + display_minutes_to + '?'+ start_to +'">' + display_hours_to + ":" + display_minutes_to + ( hours_to >= 12 ? ' pm' : ' am') + '</option>';
														start_to += 900;
													}
												table += '</select>';
										   table += '</td>';
									   table += '</tr>';
									    table += '<td width="50" height="18"></td>';
										   table += '<td width="10"></td>';
										   table += '<td width="85">'+
											   			'<div id="save_time_temp">'+
											   			'<img style="cursor:pointer" onclick="insertTempTimeSchedule(\'' + str_year + '\')" title="Save" src="./application/images/icon/save.png" border="0"/>&nbsp;&nbsp;<img style="cursor:pointer" onclick="showInsertTimeButton()" title="Cancel" src="./application/images/icon/close.png" border="0"/>'+
											   			'</div>'+
											   			'<div id="save_time_temp_loading" class="display-none" style="font-weight:normal;">'+
											   			'<img src="./application/images/icon/loading_round.gif" border="0"/> Saving...'+
											   			'</div>'+
											   		 '</td>';
									   table += '</tr>';
								table += '</table>';
									
					   table += '</td>'+
								'<td class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td class="schedule-label-time schedule-label-right">&nbsp;</td>'+
								'<td class="schedule-label-time">&nbsp;</td>'+
							 '</tr>';
				$('#schedule_table').append(table);

			}, 'json');
			
		}
		
		function showInsertTimeButton() {
			window.location.href = window.location.href;
		}
		
		function insertTempTimeSchedule(activeYear, timeInterval) {
			
			var schedFrom 		= $('#sched_from :selected').val().split('?');
				timeFrom		= schedFrom[0];
				intervalFrom	= schedFrom[1];
			var schedTo 		= $('#sched_to :selected').val().split('?');
				timeTo			= schedTo[0];
				intervalTo		= schedTo[1];
			var interval = parseFloat( parseInt(intervalTo - intervalFrom) / 3600 );
			
			$.post('request.php', {AJAXPost:'getTempLastSchedule', year_id:activeYear}, function(res) {
				
				if (  timeFrom >= timeTo ) {
					alert("Invalid Time Input.");
					return false;
				}
				else if ( timeFrom < res['sched_to'] ) {
					alert("Invalid Time Input.");
					return false;
				}
				else {
					$('#save_time_temp').hide();
					$('#save_time_temp_loading').show();
					$.post('request.php', {AJAXPost:'SaveTempTimeSchedule', year_id:activeYear, sched_from:timeFrom, sched_to:timeTo, sched_key: parseInt(parseInt(res['sched_key']) + 1), sched_time:interval}, function(res) {
						window.location.href = window.location.href;
					}, 'json');
				}
			}, 'json');
			
		}
		
		function setTimeSchedule_INTERVAL__(str_year) {
			$('#set_time_schedule').html('<div class="set-div-sched"><div class="set-div-sched-div"><ul>'+
											'<li><label onclick="OneHalfSchedule(0, \'' + str_year + '\');"><input type="radio" name="set_time_interval" value="0"/>30 Minutes Interval</label></li>'+
											'<li><label onclick="OneHalfSchedule(1, \'' + str_year + '\');"><input type="radio" name="set_time_interval" value="1"/>1 Hour Interval</label></li>'+
											'<li><label onclick="OneHalfSchedule(2, \'' + str_year + '\');"><input type="radio" name="set_time_interval" value="2"/>1 & 1/2 Hours Interval</label></li>'+
										 '</ul></div></div><div id="time_schedule"></div>');
		}
		
		function OneHalfSchedule(str_interval, str_year) {
			
			var data1 = [ '7:30', '8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00', '4:30', '5:00', '5:30', '6:00', '6:30', '7:00', '7:30', '8:00', '8:30' ];
			var data2 = [ '8:00', '8:30', '9:00', '9:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '1:00', '1:30', '2:00', '2:30', '3:00', '3:30', '4:00', '4:30', '5:00', '5:30', '6:00', '6:30', '7:00', '7:30', '8:00', '8:30', '9:00' ];
			
			var data3 = [ '7:30', '8:30', '9:30', '10:30', '11:30', '12:30', '1:30', '2:30', '3:30', '4:30', '5:30', '6:30', '7:30', '8:30' ];
			var data4 = [ '8:30', '9:30', '10:30', '11:30', '12:30', '1:30', '2:30', '3:30', '4:30', '5:30', '6:30', '7:30', '8:30', '9:30' ];
			
			var data5 = [ '7:30', '9:00', '10:30', '12:00', '1:00', '2:30', '4:00', '5:30', '7:00' ];
			var data6 = [ '9:00', '10:30', '12:00', '1:00', '2:30', '4:00', '5:30', '7:00', '8:30' ];
			
			if ( str_interval == 0 ) timeTo = '30 Minutes Interval';
			else if ( str_interval == 1 ) timeTo = '1 Hour Interval';
			else if ( str_interval == 2 ) timeTo = '1 & 1/2 Hours Interval';
			
			var table = '<div class="preview-schedule"><div>' + timeTo + ' :: Schedule Time Preview :: <span id="save_schedule" onclick="saveTimeSchedule(\''+ str_interval +'\', \''+ str_year +'\');">SAVE</span></div><div id="preview_schedule_waiting" class="preview-schedule-waiting display-none"><img src="./application/images/icon/loading_round.gif" border="0" /> Please wait...</div></div>'+
						'<table width="0" border="0" cellspacing="0" cellpadding="0" class="schedule-table">'+
						 '<tr>'+
							'<td width="120" height="25" class="schedule-label schedule-label-right" colspan="3">&nbsp;</td>'+
							'<td width="105" class="schedule-label schedule-label-right">Monday</td>'+
							'<td width="105" class="schedule-label schedule-label-right">Tuesday</td>'+
							'<td width="105" class="schedule-label schedule-label-right">Wenesday</td>'+
							'<td width="105" class="schedule-label schedule-label-right">Thursday</td>'+
							'<td width="105" class="schedule-label schedule-label-right">Friday</td>'+
							'<td width="105" class="schedule-label">Saturday</td>'+
						 '</tr>';
			if ( str_interval == 0 )	 
				for (i=0; i < data1.length; i++) {
				 table += '<tr>'+
							'<td width="50" height="75" class="schedule-label-time" align="center">' + data1[i] + '</td>'+
							'<td width="20" height="75" class="schedule-label-time" align="center">-</td>'+
							'<td width="50" class="schedule-label-time schedule-label-right" align="center">' + data2[i] + '</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time">&nbsp;</td>'+
						 '</tr>';
				}
				
			else if ( str_interval == 1 )	 
				for (i=0; i < data3.length; i++) {
				 table += '<tr>'+
							'<td width="50" height="75" class="schedule-label-time" align="center">' + data3[i] + '</td>'+
							'<td width="20" height="75" class="schedule-label-time" align="center">-</td>'+
							'<td width="50" class="schedule-label-time schedule-label-right" align="center">' + data4[i] + '</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time">&nbsp;</td>'+
						 '</tr>';
				}
				
			else if ( str_interval == 2 )	 
				for (i=0; i < data5.length; i++) {
				 table += '<tr>'+
							'<td width="50" height="75" class="schedule-label-time" align="center">' + data5[i] + '</td>'+
							'<td width="20" height="75" class="schedule-label-time" align="center">-</td>'+
							'<td width="50" class="schedule-label-time schedule-label-right" align="center">' + data6[i] + '</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time schedule-label-right">&nbsp;</td>'+
							'<td width="50" height="75" class="schedule-label-time">&nbsp;</td>'+
						 '</tr>';
				}
				
				table += '</table>';
						 
			$('#time_schedule').html(table);
		}
		
			function saveTimeSchedule(actionID, activeYear) {
				$('#save_schedule').hide();
				$('#preview_schedule_waiting').show();
				$.post('request.php', {AJAXPost:'SaveTimeSchedule', action_id:actionID, year_id:activeYear}, function() {
					window.location.href = window.location.href;
				});
			}